﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
//using System.Data.SqlServerCe;
using System.Data;
using System.Data.OleDb;
//using ErikEJ.Sql;



namespace PAPI_Import
{
    partial class Program
    {
        //StreamWriter writer = new StreamWriter("..\\..\\App_Data\\Import_Log.txt", true);

        static int Main(string[] args)
        {
            AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory + "..\\..\\..\\JustMVCTest\\App_Data");

            //TODO: Check if DB is accessiable
            
            //TODO: Fill Mail recepients
            adminRecepients.Add("flame2262@gmail.com");
            //adminRecepients.Add("gitit.mil@gmail.com");

            //PAPI_Import.exe <Mode>
            //<Mode>: 1- End Of month, 2- Daily, 3- 20th of the month

            if (args.Count() != 1)
            {
                PapiImportSendMail(MailType.ErrorMail, adminRecepients,
                                    "Hello, error with the number of arguments.",true);
                return -1;
            }

            bool result = Int32.TryParse(args[0], out PapiImportWorkMode);
            if (!result)
            {
                PapiImportSendMail(MailType.ErrorMail, adminRecepients,
                                     "Hello, argument is not numeric",true);
                return -1;
            }

            /*
            System.Diagnostics.Debug.WriteLine("args count: " + args.Count());
            for (int i = 0; i < args.Count(); i++)
            {
                System.Diagnostics.Debug.WriteLine("args[" + i + "]:" + args[i]);
            }
            */

            /*
            try
            {
             */
                writer = new StreamWriter("..\\..\\App_Data\\Import_Log.txt", true);
                //StreamWriter writer= new StreamWriter("C:\\webs\\papi_import\\PAPI_Import\\App_Data\\Import_Log.txt", true);
                //StreamWriter writer = new StreamWriter("..\\..\\..\\JustMVCTest\\App_Data\\Import_Log.txt", true);

                writeToLog("Procedure starts with mode " + PapiImportWorkMode);
                
                switch (PapiImportWorkMode)
                {
                    case 1: //done. tested.
                    {
                        OperateWorkMode1();  // Merging SAP output with PAPI DB: New POes, Closed POes, Updated POes
                        break;
                    }
                    case 2: //done. tested.
                    {
                        OperateWorkMode2();  // Checking Delivery Date and escalate. MUST RUN DAILY / DUAL-DAILY.
                        break;
                    }
                    case 3: //done. tested.
                    {
                        OperateWorkMode3(); // Percentage Check and escalate. MUST RUN DAILY / DUAL-DAILY.
                        break;
                    }
                    case 4: //done. tested.
                    {
                        OperateWorkMode4();  //Invite PO Owners to update their data. Invites all when invoked regardless of date. Groups invites by owner email.
                        break;
                    }
                    case 5: //done. tested.
                    {
                        OperateWorkMode5(); // Sending a report to TSSC with the changes of last month (1st of month)
                        // Must be run on the NEW month
                        break;
                    }
                    case 6: //done. tested.
                    {
                        OperateWorkMode6();  // Monitoring TSSC activity
                        // Must be run on the NEW month
                        break;
                    }
                    case 7: //done. tested.
                    {
                        OperateWorkMode7(); // Sending a report with untreated Open POes: Delivery Date & Percentage problem. Runs on the 28th.
                        break;
                    }
                }

                writeToLog("Procedure ends");
                writer.Close();
                
            /*
            }
            catch (IOException e)
            {
                Console.WriteLine("IOException. {0} Exception caught.", e);
                return -1;
            }

            catch (Exception e)
            {
                Console.WriteLine("Generel Exception. {0} Exception caught.", e);
                return -1;
            } 
             */

            Console.WriteLine("SUCCESS");
            
            return 0;
            //Console.ReadKey(true);
           
        }


        public class NoUsersException : System.Exception
        {
            public NoUsersException()
            {
            }

            public NoUsersException(string message)
                : base(message)
            {
            }

            public NoUsersException(string message, System.Exception innerException)
                : base(message, innerException)
            {
            }
        }


    }
}
